package com.ssy.lingxi.report.controller;

import cn.hutool.core.date.DateTime;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.report.api.vo.request.*;
import com.ssy.lingxi.report.api.vo.response.ActiveMerchantVO;
import com.ssy.lingxi.report.api.vo.response.PurchasePortalStatisticsVO;
import com.ssy.lingxi.report.entity.report.PopularShopDO;
import com.ssy.lingxi.report.model.vo.request.UpdateMemberRegisterReportVO;
import com.ssy.lingxi.report.model.vo.request.UpdateNewlyAddedDayReportVO;
import com.ssy.lingxi.report.service.*;
import com.ssy.lingxi.report.service.tally.ITallyReportService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 报表内部接口
 * @author 伍将
 * @date 2020/11/18
 **/
@RestController
@RequestMapping("/report/feign")
public class ReportFeignController {

    @Resource
    private IMemberHomeService memberHomeService;

    @Resource
    private ITallyReportService tallyReportService;

    @Resource
    private IMemberSettlementService memberSettlementService;

    @Resource
    private INewlyAddedDayReportService newlyAddedDayReportService;

    @Resource
    private IPlatformSettlementService platformSettlementService;

    @Resource
    private IPopularShopService popularShopService;

    @Resource
    private IOrderReportService orderReportService;

    @Resource
    private IMemberReportService memberReportService;

    @Resource
    private IOrderAddRecordService orderAddRecordService;

    @Resource
    private IActiveMerchantService activeMerchantService;

    /**
     * 批量新增操作通知
     * @param operateVOList 操作通知
     * @return 操作结果
     */
    @RequestMapping("/addOperateNoticeList")
    public Wrapper<Void> addOperateNoticeList(@RequestBody @Valid List<OperateNoticeVO> operateVOList) {
        return memberHomeService.addOperateNoticeList(operateVOList);
    }

    /**
     * 新增操作通知
     * @param operateVO 操作通知
     * @return 操作结果
     */
    @RequestMapping("/addOperateNotice")
    public Wrapper<Void> addOperateNoticeList(@RequestBody @Valid OperateNoticeVO operateVO) {
        return memberHomeService.addOperateNotice(operateVO);
    }

    /**
     * 新增每日订单
     * @author 伍将
     * @date 2020/12/8
     **/
    @RequestMapping("/addOrder")
    public Wrapper<Void> addOrder(@RequestBody @Valid AddOrderVO addOrderVO) {

        // 更新每日信息报表
        newlyAddedDayReportService.addOrder(addOrderVO);

        // 更新订单报表
        orderReportService.addOrder(addOrderVO);

        // 新增订单新增记录
        orderAddRecordService.add(addOrderVO);

        return Wrapper.success();
    }

    /**
     * 新增每日订单
     * @author 伍将
     * @date 2021/2/4
     * @param addOrderVO: 新增订单参数
     * @return 操作结果
     **/
    @RequestMapping("/addOrderList")
    public Wrapper<Void> addOrderList(@RequestBody @Valid List<AddOrderVO> addOrderVO) {

        // 更新每日信息报表
        newlyAddedDayReportService.addOrderList(addOrderVO);

        // 更新订单报表
        orderReportService.addOrderList(addOrderVO);

        // 新增订单新增记录
        orderAddRecordService.addList(addOrderVO);

        return Wrapper.success();
    }

    /**
     * 统计计数
     * @return 统计计数
     */
    @RequestMapping("/statisticsTally")
    public Wrapper<Void> statisticsTally() {
        tallyReportService.statisticsTally();
        return Wrapper.success();
    }

    /**
     * 会员结算
     * @author 伍将
     * @date 2020/11/27
     **/
    @RequestMapping("/memberSettlement")
    public Wrapper<Void> memberSettlement() {
        memberSettlementService.memberSettlementReport(DateTime.now());
        tallyReportService.statisticsTally();
        return Wrapper.success();
    }

    /**
     * 平台结算
     * @author 伍将
     * @date 2020/12/14
     **/
    @RequestMapping("/platformSettlement")
    public Wrapper<Void> platformSettlement() {
        platformSettlementService.settlementReport(DateTime.now());
        tallyReportService.statisticsTally();
        return Wrapper.success();
    }

    /**
     * 会员票据报表
     * @author 伍将
     * @date 2020/12/15
     **/
    @RequestMapping("/memberReceiptInvoiceReport")
    public Wrapper<Void> memberReceiptInvoiceReport() {
        memberSettlementService.memberReceiptInvoiceReport();
        tallyReportService.statisticsTally();
        return Wrapper.success();
    }

    /**
     * 会员票据通知
     * @author 伍将
     * @date 2020/12/22
     **/
    @RequestMapping("/memberReceiptInvoiceNotice")
    public Wrapper<Void> memberReceiptInvoiceNotice() {
        memberSettlementService.memberReceiptInvoiceNotice();
        return Wrapper.success();
    }

    /**
     * 获取人气店铺Id
     * @author 伍将
     * @date 2020/12/28
     **/
    @RequestMapping("/getPopularShopIdList")
    public Wrapper<List<Long>> getPopularShopIdList(@RequestBody @Valid PopularShopRequest request) {
        return popularShopService.getPopularShopIdList(request);
    }

    /**
     * 获取人气店铺信息
     * @author lqa
     * @date 2021/11/18
     **/
    @RequestMapping("/getPopularShopList")
    public Wrapper<List<PopularShopDO>> getPopularShopList(@RequestBody @Valid PopularShopRequest request) {
        return popularShopService.getPopularShopListMsg(request);
    }

    /**
     * 更新每日新增报表
     * @author 伍将
     * @date 2021/1/26
     **/
    @RequestMapping("/updateNewlyAddedDayReport")
    public Wrapper<Void> updateNewlyAddedDayReport(@RequestBody @Valid UpdateNewlyAddedDayReportVO request) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime startTime = LocalDateTime.parse(request.getStartTime() + " 00:00:00", formatter);
        LocalDateTime endTime = LocalDateTime.parse(request.getEndTime() + " 23:59:59", formatter);
        newlyAddedDayReportService.newlyAddedDayReport(startTime, endTime);

        return Wrapper.success();
    }

    /**
     * 更新会员注册日报表
     * @author 伍将
     * @date 2021/1/29
     **/
    @RequestMapping("/updateMemberRegisterDayReport")
    public Wrapper<Void> updateMemberRegisterDayReport(@RequestBody @Valid UpdateMemberRegisterReportVO request) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startTime = LocalDateTime.parse(request.getStartTime() + " 00:00:00", formatter);
        LocalDateTime endTime = LocalDateTime.parse(request.getEndTime() + " 23:59:59", formatter);
        memberReportService.memberRegisterDayReport(startTime, endTime);

        return Wrapper.success();
    }

    /**
     * 更新会员注册月报表
     * @author 伍将
     * @date 2021/1/29
     **/
    @RequestMapping("/updateMemberRegisterMonthReport")
    public Wrapper<Void> updateMemberRegisterMonthReport(@RequestBody @Valid UpdateMemberRegisterReportVO request) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startTime = LocalDateTime.parse(request.getStartTime() + " 00:00:00", formatter);
        LocalDateTime endTime = LocalDateTime.parse(request.getEndTime() + " 23:59:59", formatter);
        memberReportService.memberRegisterMonthReport(startTime, endTime);

        return Wrapper.success();
    }

    /**
     * 店铺-活跃商户
     * @author ds
     * @date 2020/11/3
     **/
    @RequestMapping(value = "/getActiveMerchantList")
    public Wrapper<List<ActiveMerchantVO>> getActiveMerchantList() {
        return activeMerchantService.getActiveMerchantList();
    }

    /**
     *获得采购门户-采购信息统计
     * @return 采购门户-采购信息统计
     */
    @RequestMapping(value = "/getPurchasePortalStatisticsInfo")
    public Wrapper<PurchasePortalStatisticsVO> getPurchasePortalStatisticsInfo(@RequestBody @Valid PurchasePortalRequest request) {
        return memberReportService.getPurchaseStatisticsByPortal(request);
    }

    /**
     *获得采购门户-采购信息统计
     * @return 采购门户-采购信息统计
     */
    @RequestMapping(value = "/getPurchasePortalStatisticsInfoList")
    public Wrapper<List<PurchasePortalStatisticsVO>> getPurchasePortalStatisticsInfoList(@RequestBody @Valid List<PurchasePortalRequest> request) {
        return memberReportService.getPurchaseStatisticsByPortal(request);
    }

    /**
     * 新增下级会员申请
     * @param addOrderVO 新增订单参数
     * @return 操作结果
     */
    @RequestMapping("/addSubMember")
    Wrapper<Void> addSubMember(@RequestBody @Valid AddSubMemberVO addOrderVO){
        return memberReportService.addSubMemberRegisterReport(addOrderVO);
    }
}
